<template>
  <section>
    <!--高级查询和修改工具条aaaaa-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyWords" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-table :data="listDatas" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="公司名" width="100" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="100"  sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="启用状态" width="100" sortable>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="100" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="商标" width="100" sortable>
      </el-table-column>
      <el-table-column prop="admin_id" label="管理员" width="100" sortable>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination :page-sizes="sizeList" @size-change="handleSizeChange"
                     layout="total,sizes,prev, pager, next,jumper"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="total" style="float:right;">

      </el-pagination>
    </el-col>
    <!--新增编辑界面-->
    <el-dialog title="操作" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="shop" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="公司名" prop="name">
          <el-input v-model="shop.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="shop.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-input v-model="shop.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="shop.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商标" prop="logo">
          <el-input v-model="shop.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员" prop="logo">
          <el-input v-model="shop.admin_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="shop.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
export default {
  data() {
    return {
      //用于高级查询绑定
      filters: {
        keyWords:''
      },
      listLoading:false,
      listDatas:[],
      sels: [],
      total:0,
      pageSize:5,
      currentPage:1,
      sizeList:[5, 10, 15, 20],
      // 控制模态框打开或关闭
      addFormVisible:false,
      shop:{
        id:null,
        name:'',
        tel:'',
        registerTime:'',
        state:0,
        address:'',
        logo:'',
        admin_id:null
      },
      addLoading:false,
      //定义校验规则
      addFormRules: {
        name: [
          { required: true, message: '请输入公司名', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    handleEdit(index,row){
      this.shop = Object.assign({}, row);
      this.addFormVisible=true;
    },
    // 新增弹出框点击事件
    addSubmit(){
      this.$refs.addForm.validate((valid) => {
        // 表单提交的数据是否符合验证规则
        if (valid) {
          this.$http.put("/shop",this.shop).then(res=>{
            let {success,message}=res.data;
            if (success){
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
            }else {
              this.$message.error("网络异常");
            }
            //刷新列表数据
            this.loadListData();
            //关闭模态框
            this.addFormVisible=false;
          })
        }
      });
    },
    // 显示新增界面
    handleAdd() {
      this.shop = {
        id:null,
        name:'',
        tel:'',
        registerTime:'',
        state:0,
        address:'',
        logo:'',
        admin_id:null
      };
      this.addFormVisible = true;
    },
    // 批量删除
    handleDel(index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //用户点了确认删除
        this.$http.delete("/shop/"+row.id).then(res=>{
          console.debug(res)
          let {success, message}=res.data;
          if(success){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }else {
            this.$message.error("网络异常");
          }
          //重新加载列表
          this.loadListData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSizeChange(val){
      this.pageSize = val;
      //重新加载列表
      this.loadListData();
    },
    //val 就是要展示的当前页
    handleCurrentChange(val){
      //动态改变currentPage 页码
      this.currentPage =val;
      //重新加载列表
      this.loadListData();
    },
    //批量删除
    batchRemove(){
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.patch("/shop",ids).then(res=>{
          let {success, message}=res.data;
          if(success){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }else {
            this.$message.error("网络异常");
          }
          //重新加载列表
          this.loadListData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //状态显示转换
    formatState: function (row, column) {
      return row.state == 0 ? '启用': '禁用';
    },
    //当用户勾选数据的时候触发
    selsChange: function (sels) {
      this.sels = sels;
    },
    //加载列表数据
    loadListData(){
      //准备请求数据
      let param={
        currentPage:this.currentPage,
        pageSize:this.pageSize,
        keyWords:this.filters.keyWords
      }
      //发送axios请求 加载全部数据
      this.$http.post("/shop",param).then(res=>{
        this.listDatas=res.data.data;
        this.total=res.data.count;
      })
    }
  },
  mounted() {
    //页面加载完毕以后立即执行
    this.loadListData();
  }
}
</script>

<style scoped>

</style>